/* **************************************************
 * User Group Manager
 *      author:     Baskin Tapkan
 *          written for CodePlex
 *              April - 2008
 *                  http://tapkan.com
 * ****************************************************
 */

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

namespace UserGroupManager
{
    public partial class DeleteUser : Form
    {
        string computerName = string.Empty;
        DataSet userInfo;
        int selectedIndex = 0;

        public DeleteUser(string systemName)
        {
            computerName = systemName;
            InitializeComponent();
            this.Text = string.Format("Delete user for {0}", computerName);
        }

        // delete account click
        private void btnDelete_Click(object sender, EventArgs e)
        {
            // instantiate the UserMgr class
            UserMgr um = new UserMgr();
            um.ComputerName = computerName;

            string confMsg = string.Format("Do you really want to delete account {0}?", cmbUserName.Text);
            DialogResult confirm = MessageBox.Show(confMsg, "Confirm", MessageBoxButtons.YesNo, MessageBoxIcon.Question);

            if (confirm == DialogResult.Yes)
            {
                bool result = um.DeleteAccount(cmbUserName.Text);
                staUserMgr.Items[0].Text = "Account deleted";
                userInfo.Tables[0].Clear();
                userInfo = um.GetAllLocalUsers();
                ResetUI();
                cmbUserName.Text = "";
                txtFullName.Text = "";
                txtDescription.Text = "";
            }
        }

        private void btnClose_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void ResetUI()
        {
            cmbUserName.Items.Clear();
            for (int i = 0; i < userInfo.Tables[0].Rows.Count; i++)
                cmbUserName.Items.Add(userInfo.Tables[0].Rows[i]["UserName"].ToString());
        }

        private void UpdateUI(int rowIndex)
        {
            cmbUserName.Text = userInfo.Tables[0].Rows[rowIndex]["UserName"].ToString();
            txtFullName.Text = userInfo.Tables[0].Rows[rowIndex]["FullName"].ToString();
            txtDescription.Text = userInfo.Tables[0].Rows[rowIndex]["Description"].ToString();
        }

        private void DeleteUser_Load(object sender, EventArgs e)
        {
            UserMgr um = new UserMgr();
            um.ComputerName = computerName;
            userInfo = um.GetAllLocalUsers();

            // make sure dataset comes back with records
            if (userInfo.Tables[0].Rows.Count > 0)
            {
                UpdateUI(0);
                for (int i = 0; i < userInfo.Tables[0].Rows.Count; i++)
                    cmbUserName.Items.Add(userInfo.Tables[0].Rows[i]["UserName"].ToString());
                cmbUserName.SelectedIndex = selectedIndex;
            }
        }

        private void cmbUserName_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (userInfo.Tables[0].Rows.Count > 0)
            {
                selectedIndex = cmbUserName.SelectedIndex;
                UpdateUI(selectedIndex);
                selectedIndex = cmbUserName.SelectedIndex;
            }
        }
    }
}